using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class Admin_EmployeeDetails : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)
            FillData();
    }

    private void FillData()
    {
        // Connection
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Con"].ToString());
        
        con.Open();

        SqlDataReader dr;

        String temp = Request.QueryString["username"] ;
        // Prepare command
        SqlCommand cmd = new SqlCommand("select * from Employee where username = @username" , con);
        cmd.Parameters.AddWithValue("username", temp);

        // Execute query
        dr = cmd.ExecuteReader();

        // Start filling data
        while (dr.Read())
        {
            TextBox1.Text = dr.GetString(0);
            TextBox2.Text = dr.GetString(1);
            TextBox3.Text = dr.GetString(2);
            TextBox4.Text = dr.GetDateTime(3).ToShortDateString();
            DropDownList1.SelectedValue = dr.GetBoolean(4).ToString();
            TextBox6.Text = dr.GetString(5);
            TextBox7.Text = dr.GetString(6);
            TextBox8.Text = dr.GetString(7);
        }

        con.Close();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        
        try
        {
            //Update in the DB
            // New connection
            Connection newCon = new Connection();
            SqlConnection con = newCon.ConStr;

            // open conneciton
            con.Open();

            // prepare command
            SqlCommand cmd = new SqlCommand("update Employee set password = @pass , fullname = @fullname , birthdate = @bdate , gender = @sex , email = @email , address = @address , tel = @tel where username = @username", con);

            cmd.Parameters.AddWithValue("username" , TextBox1.Text);
            cmd.Parameters.AddWithValue("pass", TextBox2.Text);
            cmd.Parameters.AddWithValue("fullname", TextBox3.Text);
            cmd.Parameters.AddWithValue("bdate", TextBox4.Text);
            cmd.Parameters.AddWithValue("sex", DropDownList1.SelectedValue);
            cmd.Parameters.AddWithValue("email", TextBox6.Text);
            cmd.Parameters.AddWithValue("address", TextBox7.Text);
            cmd.Parameters.AddWithValue("tel", TextBox8.Text);

            // execute query
            cmd.ExecuteNonQuery();

            // close connection
            con.Close();
            Label1.Text = "";
            
            // Update in the ASP DB
            MembershipUser mu = Membership.GetUser(TextBox1.Text);
            mu.ChangePassword(mu.ResetPassword(), TextBox2.Text);
            Membership.UpdateUser(mu);
        }
        catch (Exception ex)
        {
            Label1.Text = "Cannot update record: " + ex.Message;
        }


    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Admin/Employee.aspx");
    }
}
